﻿using System;
using System.IO;

namespace Nakio.Framework.Communication
{
    /// <summary>
    /// Command executed by the client to send the missing assembly to the server.
    /// </summary>
    [Serializable]
    internal class MissingAssemblyCommand : CommandBase
    {
        private readonly string _assemblyFullName;

        /// <summary>
        /// Constructor instanciated by the server
        /// </summary>
        /// <param name="assemblyFullName">The missing assembly FullName</param>
        public MissingAssemblyCommand(string assemblyFullName)
        {
            _assemblyFullName = assemblyFullName;
        }

        /// <summary>
        /// Send the assembly to the Server
        /// </summary>
        public override object Execute()
        {
            var assemblies = AppDomain.CurrentDomain.GetAssemblies();
 
            foreach (var assembly in assemblies)
            {
                if (assembly.FullName == _assemblyFullName)
                {
                    var assemblyBytes = File.ReadAllBytes(assembly.Location);
                    return assemblyBytes;
                }
            }
            return null;
        }
    }
}